package com.briup.util;

import com.briup.constant.SystemConstant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JWTUtils {
    private static final String SIGNINGKEY = "briup";
    private static final long EXPIRATION = SystemConstant.ONE_DAY_CONVERSION_MILLISECOND;

    public static String generator(Map<String, Object> map) {
        String token = Jwts.builder()
                .addClaims(map)
                .signWith(SignatureAlgorithm.HS256, SIGNINGKEY)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION))
                .compact();
        return token;
    }

    public static Object parse(String token) {
        Object body = Jwts.parser()
                .setSigningKey(SIGNINGKEY)
                .parse(token)
                .getBody();
        return body;
    }

    public static Claims paresToken(String token) {
        Claims claims = Jwts.parser()
                //指定签名秘钥
                .setSigningKey(SIGNINGKEY)
                //指定JWT令牌
                .parseClaimsJws(token)
                .getBody();
        return claims;
    }
}
